package com.wkg.lotterysystem.controller;

import com.wkg.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.wkg.lotterysystem.common.exception.ControllerException;
import com.wkg.lotterysystem.common.pojo.CommonResult;
import com.wkg.lotterysystem.common.utils.JacksonUtils;
import com.wkg.lotterysystem.controller.param.CreateActivityParam;
import com.wkg.lotterysystem.controller.param.PageParam;
import com.wkg.lotterysystem.controller.result.CreateActivityResult;
import com.wkg.lotterysystem.controller.result.FindActivtyListResult;
import com.wkg.lotterysystem.controller.result.GetActivityDetailResult;
import com.wkg.lotterysystem.service.ActivityService;
import com.wkg.lotterysystem.service.dto.ActivityDTO;
import com.wkg.lotterysystem.service.dto.ActivityDetailDTO;
import com.wkg.lotterysystem.service.dto.CreateActivityDTO;
import com.wkg.lotterysystem.service.dto.PageListDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.stream.Collectors;

@RestController
public class ActivityController {
    private static final Logger log = LoggerFactory.getLogger(ActivityController.class);

    @Autowired
    private ActivityService activityService;

    //user 50 51
    //prize 18 19
    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResult> createActivity(@Validated @RequestBody CreateActivityParam param) {
        log.info("createActivity  CreateActivityParam : {} ", JacksonUtils.writeValueAsString(param));
        return CommonResult.success(convertToCreateActivityResult(activityService.createActivity(param)));

    }

    @RequestMapping("/activity/find-list")
    public CommonResult<FindActivtyListResult> findActivtyList(PageParam param) {
        log.info("createActivity  CreateActivityParam : {} ", JacksonUtils.writeValueAsString(param));
        return CommonResult.success(convertToFindActivityListResult(activityService.findActivtyList(param)));
    }

    @RequestMapping("/activity-detail/find")
    public CommonResult<GetActivityDetailResult> getActivityDetail(Integer activityId) {
        log.info("getActivityDetail  activityId：{}", activityId);
        return CommonResult.success(convertToGetActivityDetailResult(
                activityService.getActivityDetail(activityId)));
    }



    private GetActivityDetailResult convertToGetActivityDetailResult(ActivityDetailDTO detailDTO) {
        if (null == detailDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.GET_ACTIVITY_DETAIL_ERROR);
        }
        GetActivityDetailResult result = new GetActivityDetailResult();
        result.setActivityId(detailDTO.getActivityId());
        result.setActivityName(detailDTO.getActivityName());
        result.setDescription(detailDTO.getDesc());
        result.setValid(detailDTO.valid());

        result.setPrizes(
                detailDTO.getPrizeDTOList().stream()
                        .sorted(Comparator.comparing(prizeDTO -> prizeDTO.getTiers().getCode()))
                        .map(prizeDTO -> {
                            GetActivityDetailResult.prizes prizes = new GetActivityDetailResult.prizes();
                            prizes.setPrizeId(prizeDTO.getPriceId());
                            prizes.setName(prizeDTO.getName());
                            prizes.setImageUrl(prizeDTO.getImageUrl());
                            prizes.setPrice(prizeDTO.getPrice());
                            prizes.setDescription(prizeDTO.getDescription());
                            prizes.setPrizeTierName(prizeDTO.getTiers().getMessage());
                            prizes.setPrizeAmount(prizeDTO.getPrizeAmount());
                            prizes.setValid(prizeDTO.valid());
                            return prizes;
                        }).collect(Collectors.toList())
        );
        result.setUsers(detailDTO.getUserDTOList()
                .stream()
                .map(userDTO -> {
                    GetActivityDetailResult.users users = new GetActivityDetailResult.users();
                    users.setUserId(userDTO.getUserId());
                    users.setUserName(userDTO.getUserName());
                    users.setValid(userDTO.valid());
                    return users;
                }).collect(Collectors.toList())
        );
        return result;
    }


    private FindActivtyListResult convertToFindActivityListResult(PageListDTO<ActivityDTO> activtyList) {
        if (null == activtyList) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_ACTIVITY_ERROR);
        }
        FindActivtyListResult result = new FindActivtyListResult();
        result.setTotal(activtyList.getTotal());
        result.setRecords(
                activtyList
                        .getRecords()
                        .stream()
                        .map(activityDTO -> {
                            FindActivtyListResult.ActivityInfo activityInfo = new FindActivtyListResult.ActivityInfo();
                            activityInfo.setActivityId(activityDTO.getActivityId());
                            activityInfo.setActivityName(activityDTO.getActivityName());
                            activityInfo.setDescription(activityDTO.getDescription());
                            activityInfo.setValid(activityDTO.valid());
                            return activityInfo;
                        }).collect(Collectors.toList())
        );
        return result;
    }


    private CreateActivityResult convertToCreateActivityResult(CreateActivityDTO createActivityDTO) {
        if (null == createActivityDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.CREATE_ACTIVITY_ERROR);
        }
        CreateActivityResult createActivityResult = new CreateActivityResult();
        createActivityResult.setActivityId(createActivityDTO.getActivityId());
        return createActivityResult;
    }
}
